package com.bai.serviceOA.Controller;

import com.bai.Springsecurity.custom.CustomUser;
import com.bai.common.JWT.JwtHelper;
import com.bai.common.Result;
import com.bai.common.Utils.MD5;
import com.bai.model.system.SysUser;
import com.bai.model.vo.LoginVo;
import com.bai.model.vo.RouterVo;
import com.bai.serviceOA.service.SysMenuService;
import com.bai.serviceOA.service.SysUserService;
import com.bai.serviceOA.service.impl.SysUserServiceImpl;
import com.bai.serviceUtil.erro.BaiExcetion;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.message.callback.PrivateKeyCallback;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    //login
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        //获取输入用户名和密码
        //根据用户名查询数据库
        CustomUser customUser = sysUserService.loadUserByUsername(loginVo.getUsername());
        //用户信息是否存在
        if (customUser == null) {
            throw new BaiExcetion(201, "用户不存在");
        }
        //判断密码
        //数据库里md5加密密码
        String password = customUser.getSysUser().getPassword();
        //获取输入的密码
        String loginVoPassword = loginVo.getPassword();
        String encrypt = MD5.encrypt(loginVoPassword);
        if (! password.equals(encrypt)) {
            throw new BaiExcetion(201, "密码错误");
        }
        //判断用户是否被禁用 1可用 0禁用
        if (customUser.getSysUser().getStatus().intValue() == 0) {
            throw new BaiExcetion(201, "用户已被禁用");
        }
        //使用jwt根据用户id和用户名称生成token字符串
        String token = JwtHelper.createToken(customUser.getSysUser().getId(), customUser.getSysUser().getUsername());
        //返回
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.ok(map);
    }


    /**
     * info：获取登录用户可以操作的token
     */
    @GetMapping("info")
    public Result info(HttpServletRequest request) {
        //1、从请求头获取用户信息(token)
        String token = request.getHeader("header");
        //2、从token中获取用户id
        Long userId = JwtHelper.getUserId(token);
        //3、根据用户id查询数据库
        SysUser user = sysUserService.getById(userId);
        //4、根据用户id获取用户可以操作的菜单列表
        //查询数据库动态构建路由结构
        List<RouterVo> routerList = sysMenuService.findUserMenuListByUserId(userId);
        //5、根据用户id获取用户可以操作的按钮列表
        List<String> permsList = sysMenuService.findUserPermsByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("roles", "[admin]");
        map.put("name", user.getName());
        map.put("avatar", "https://oss.aliyuncs.com/aliyun_id_photo_bucket/");
        map.put("routers", routerList);
        map.put("buttons", permsList);
        return Result.ok(map);
    }
}
